using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace TST.SharePoint.Shared.WebControls
{
    /// <summary>
    /// Template object for a column. To be used in a Grid object. 
    /// Render a non-clickable checkbox, based on a boolean value in the datasource.
    /// </summary>
    public class CheckBoxColumn : ITemplate
    {
        private string _boolField;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="boolFieldName">The field name of the boolean field.</param>
        public CheckBoxColumn(string boolFieldName)
        {
            _boolField = boolFieldName;
        }

        public void InstantiateIn(Control container)
        {
            if (container != null)
            {
                CheckBox select = new CheckBox();
                select.ID = "SelectSelectedField";
                select.Enabled = (string.IsNullOrEmpty(_boolField));
                select.DataBinding += new EventHandler(select_DataBinding);
                container.Controls.Add(select);
            }
        }

        void select_DataBinding(object sender, EventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            if (!string.IsNullOrEmpty(_boolField))
            {
                GridViewRow container = (GridViewRow)checkBox.NamingContainer;
                string selected = ((System.Data.DataRowView)container.DataItem)[_boolField].ToString();
                checkBox.Checked = selected.Equals("true", StringComparison.InvariantCultureIgnoreCase);
            }
        }

    }
}
